package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.GoodsDetails;
import com.aletto.general_store.presenters.core.SimpleParamsTemplate;
import com.aletto.general_store.ui.GoodsGridItemDecoration;
import com.aletto.general_store.presenters.views.GoodsDetailsView;
import com.aletto.general_store.ui.adapters.GoodsCoverBannerAdapter;
import com.aletto.general_store.ui.adapters.GoodsListAdapter;
import com.aletto.general_store.ui.popup.GoodsPopup;
import com.aletto.general_store.utils.Login;
import com.aletto.general_store.utils.GoodsCoverIndicator;
import com.aletto.general_store.utils.ToastUtils;
import com.aletto.general_store.utils.XHelper;
import com.lxj.xpopup.XPopup;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.Debounce;
import com.sisyphus.spinach.network.views.ToastView;
import com.sisyphus.spinach.utils.RichTextHelper;
import com.youth.banner.Banner;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import me.jessyan.autosize.utils.AutoSizeUtils;

/**
 * 商品详情
 */
public class GoodsDetailsActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, Goods goods) {
        return new Intent(context, GoodsDetailsActivity.class).putExtra(Goods.EXTRA_NAME, goods);
    }

    private Goods goods;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);

        findViewById(R.id.back_image).setOnClickListener(v -> finish());

        Intent intent = getIntent();
        goods = (Goods) intent.getSerializableExtra(Goods.EXTRA_NAME);

        if (goods != null) {
            renderGoodsDetails(goods);

            findViewById(R.id.iv_copy).setOnClickListener(v -> {
                XHelper.copy2Clipboard(String.valueOf(goods.id));
                ToastUtils.makeText(R.string.copy_success);
            });

            // 购买
            findViewById(R.id.button_buy).setOnClickListener(new Login(v -> {
                new XPopup.Builder(this)
                        .asCustom(new GoodsPopup(this, goods))
                        .show();
            }));

            // 收藏
            TextView favoritesButton = findViewById(R.id.button_favorites);
            favoritesButton.setOnClickListener(new Login(v ->
                    favoritesGoodsTemplate.setParams(goods.id).execute(getLifecycle())));
        }

        // 跳转搜索
        findViewById(R.id.layout_search).setOnClickListener(v ->
                startActivity(new Intent(this, SearchActivity.class)));

        // 请求商品详情
        goodsDetailsTemplate.setParams(goods.id).execute(getLifecycle());
    }

//    private void renderRecommend(List<Goods> data) {
//        RecyclerView recyclerRecommend = findViewById(R.id.recycler_recommend);
//        recyclerRecommend.addItemDecoration(new GoodsGridItemDecoration(AutoSizeUtils.dp2px(this, 4)));
//        recyclerRecommend.setAdapter(new GoodsListAdapter(data));
//    }


    private void renderGoodsDetails(Goods goods) {
        TextView goodsPriceText = findViewById(R.id.tv_goods_price);
        TextView priceUnitText = findViewById(R.id.tv_price_unit);
        TextView goodsPriceOriginText = findViewById(R.id.tv_goods_price_origin);
        TextView soldNumberText = findViewById(R.id.tv_sold_number);
        TextView goodsTitleText = findViewById(R.id.tv_goods_title);
        Banner<String, GoodsCoverBannerAdapter> goodsCoverBanner = findViewById(R.id.banner_goods_cover);

        TextView favoritesButton = findViewById(R.id.button_favorites);
        favoritesButton.setSelected(goods.is_like == 1);

        goodsPriceText.setText(XHelper.toPrice(goods.getPrice())); // 价格
        priceUnitText.setText(goods.getPriceUnit()); // 货币单位
        soldNumberText.setText(String.format(getString(R.string.placeholder_goods_sold_number), goods.getSoldNumber()));
        goodsTitleText.setText(goods.getTitle()); // 标题
        goodsPriceOriginText.setText(
                RichTextHelper.newBuilder(goods.getOriginal_price() + goods.getPriceUnit())
                        .striketrough()
                        .build()
        );

        // 封面图
        goodsCoverBanner
                .setAdapter(new GoodsCoverBannerAdapter(goods.goods_img))
                .addBannerLifecycleObserver(GoodsDetailsActivity.this)
                .setIndicator(new GoodsCoverIndicator(GoodsDetailsActivity.this))
                .start();
    }


    /**
     * 商品详情
     */
    private final SimpleParamsTemplate<GoodsDetails, String> goodsDetailsTemplate = new SimpleParamsTemplate<>() {

        @Override
        public INetTask.View<GoodsDetails> view() {
            return new ToastView<>(getApplication(), new GoodsDetailsView(findViewById(R.id.layout_parent)));
        }

        @Override
        public INetTask.Presenter<GoodsDetails, INetTask.View<GoodsDetails>> presenter() {
            return new NetSingleTask<>(DataRepository.getInstance().getGoodsDetails(params[0], 0));
        }

    };

    /**
     * 收藏商品
     */
    private final SimpleParamsTemplate<Object, String> favoritesGoodsTemplate = new SimpleParamsTemplate<>() {

        @Override
        public INetTask.View<Object> view() {
            View favoritesGoods = findViewById(R.id.button_favorites);
            return new Debounce<>(favoritesGoods, new INetTask.View<>() {
                @Override
                public void success(Object data) {
                    favoritesGoods.setSelected(!favoritesGoods.isSelected());
                }
            });
        }

        @Override
        public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
            View favoritesGoods = findViewById(R.id.button_favorites);
            return new NetSingleTask<>(DataRepository.getInstance().favoritesGoods(params[0], !favoritesGoods.isSelected()));
        }
    };

}
